package manager
{
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;

	import globals.Global;
	import globals.Setting;

	import util.DisplayUtil;

	/**
	 * PopUpManager singleton 类用于弹出新的顶级窗口,可以在位于所有其它可见窗口上面的层次中放置或删除这些窗口。
	 * <br>PopUpManager 还可以提供模态，使弹出窗口下方的窗口无法接收鼠标事件。
	 * @author luguobin
	 */
	public class PopUpManager
	{
		private static var popUpDic:Dictionary = new Dictionary();

		/**
		 * 弹出顶级窗口。
		 * @param window 要弹出的DisplayObject.
		 * @param modal  如果为 true，则该窗口为模态窗口，也就是说在删除该窗口之前，用户将无法与其它弹出窗口交互。
		 */
		public static function addPopUp(window:DisplayObject, modal:Boolean = false):void
		{
			if(window == null)
			{
				return;
			}

			if(modal)
			{
				var bgMask:BackgroundMask = new BackgroundMask();
				DisplayUtil.addChild(bgMask, Global.game.topLayer);
				addToDictionary(window, bgMask);
				BackgroundMask.top = window;
			}
			DisplayUtil.addChild(window, Global.game.topLayer);
			addToDictionary(window, window);
		}

		/**使位于调用 addPopUp() 方法期间使用的任何窗口上面的弹出窗口居中显示。**/
		public static function centerPopUp(popUp:DisplayObject):void
		{
			if(popUpDic[popUp] != undefined)
			{
				popUp.x = (Setting.STAGE_WIDTH - popUp.width) / 2;
				popUp.y = (Setting.STAGE_HEIGHT - popUp.height) / 2;
			}
		}

		/**删除由 addPopUp() 方法弹出的弹出窗口。**/
		public static function removePopUp(popUp:DisplayObject):void
		{
			if(popUpDic[popUp] != undefined)
			{
				BackgroundMask.top = null;
				var curArr:Array = popUpDic[popUp];

				for(var i:int = curArr.length - 1; i >= 0; i--)
				{
					DisplayUtil.removeChild(Global.game.topLayer, curArr[i]);
				}
				delete popUpDic[popUp];
			}
		}

		private static function addToDictionary(key:DisplayObject, window:DisplayObject):void
		{
			if(popUpDic[key] == undefined)
			{
				popUpDic[key] = [];
			}
			var curArr:Array = popUpDic[key];

			if(curArr.indexOf(window) == -1)
			{
				curArr.push(window);
			}
		}
	}
}
import flash.display.DisplayObject;
import flash.display.Sprite;

import globals.Global;
import globals.Setting;

/**蒙板**/
class BackgroundMask extends Sprite
{
	public static var top:DisplayObject;

	public function BackgroundMask()
	{
		with(this.graphics)
		{
			beginFill(0x000000, 0.3);
			drawRect(0, 0, Setting.STAGE_WIDTH, Setting.STAGE_HEIGHT);
			endFill();
		}
	}

	public function click():void
	{
		if(BackgroundMask.top)
		{
			//GameTween.shock(BackgroundMask.top, 1);
		}
	}
}